//
//  soundClient.m
//  01_kakin
//
//  Created by jon due on 12/03/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "soundClient.h"

@implementation soundClient

-(NSArray*)selectGeneral:(NSString*)sql{
    
    NSMutableArray *array = [[ NSMutableArray alloc ]init];
    if (status == DBStatusClosed) {
		[super open];
	}
    FMResultSet *rs = [innerDB executeQuery:sql];
	soundBean *Bean = nil;
	while ([rs next]) {
		Bean = [[soundBean alloc] init];
        
        Bean.No =           [ rs intForColumn: SD_NO ];
        Bean.lebel =        [ rs intForColumn:SD_LEVEL ]; 
        Bean.categoryNo =   [ rs intForColumn:SD_CATEGORY_NO ];
        Bean.word =         [ rs stringForColumn:SD_WORD ];
        Bean.fileName =     [ rs stringForColumn:SD_FILE_NAME ];
        Bean.isGet =        [ rs intForColumn:SD_IS_GET ];
        
        [array addObject:Bean];
		[Bean release];
	}
    
	[rs close];
    
	if ([innerDB hadError]) {
        NSLog(@"database path: %@", [innerDB databasePath]);
		NSLog(@"database error: %@", [innerDB lastErrorMessage]);
	}
	
	return [array autorelease];
}

-(NSArray*)allsound{
    
    NSString *sqlQuery = 
	[NSString stringWithFormat:@"SELECT * FROM %@", TB_SOUND];
    
    return [ self selectGeneral:sqlQuery ];
    
}


/* ============================================================================== */
#pragma mark -  insert method
/* ============================================================================== */

-(void)insertSound:(soundBean*)Bean{
    
   	NSString *sql = [NSString stringWithFormat:
                     @"INSERT INTO %@ (%@,%@,%@,%@,%@) VALUES(%d,%d,'%@','%@',%d)",
                     TB_SOUND,
                     SD_LEVEL,
                     SD_CATEGORY_NO,
                     SD_WORD,
                     SD_FILE_NAME,
                     SD_IS_GET,
                     Bean.lebel,
                     Bean.categoryNo,
                     Bean.word,
                     Bean.fileName,
                     Bean.isGet];
    
	[super open];
    [ innerDB executeUpdate:sql ];
    
	if ([innerDB hadError]) {
		NSLog(@"database error: %@", [innerDB lastErrorMessage]);
	}
	[super close];
}

/* ============================================================================== */
#pragma mark -  updata method
/* ============================================================================== */

// 再生flgを更新する
-(void)updateSound:(soundBean*)bean{
    
    NSString *sql = [NSString stringWithFormat:
					 @"UPDATE %@ SET %@ = ? WHERE %@ = ?",
                     TB_SOUND,
                     SD_IS_GET,
                     SD_NO];
    
    NSArray *arguments = [NSArray arrayWithObjects:
                          [ NSNumber numberWithInt:bean.isGet ],
                          [ NSNumber numberWithInt:bean.No],
                          nil ];
    
    [super open];
	[innerDB executeUpdate:sql withArgumentsInArray:arguments];
	if ([innerDB hadError]) {
		NSLog(@"database error: %@", [innerDB lastErrorMessage]);
	}
	[super close];
    
    
}



@end
